//
//  ChargeAndBalanceView.m
//  BeanSproutEDU
//
//  Created by douya on 15/1/7.
//  Copyright (c) 2015年 douya. All rights reserved.
//

#import "ChargeAndBalanceView.h"
#import "ChargeAndBalanceCell.h"
#define RGB(r,g,b)
@implementation ChargeAndBalanceView
-(id)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame])
    {
        UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-64) style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        tableView.dataSource=self;
        tableView.sectionFooterHeight=0;
        [self addSubview:tableView];
        
        RechargeAmountArray=[NSArray arrayWithObjects:@"10元",@"50元",@"100元",@"500元", nil];
        GiveMoneyArray=[NSArray arrayWithObjects:@"",@"赠送5元",@"赠送15元",@"赠送50元", nil];
        PayStyleArray=[NSArray arrayWithObjects:@"支付宝",@"银行卡",@"礼品卡", nil];
        PayDescArray=[NSArray arrayWithObjects:@"",@"",@"了解礼品卡", nil];
        
        ChargeSelectCellIndex=1;
    }
    return self;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0)
    {
        return 1;
    }
    else if (section==1)
    {
        return 4;
    }
    else
    {
        return 4;
    }
}
-(ChargeAndBalanceCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID=@"cell1";
    static NSString *ID1=@"cell2";
    ChargeAndBalanceCell *cell=nil;
    cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell)
    {
        NSArray *array=[[NSBundle mainBundle] loadNibNamed:@"ChargeAndBalanceCell" owner:nil options:nil];
        cell=array[0];
    }
    
    if (indexPath.section==2&&indexPath.row==3)//支付btn的cell
    {
        cell=[tableView dequeueReusableCellWithIdentifier:ID1];
        if (!cell)
        {
            NSArray *array=[[NSBundle mainBundle] loadNibNamed:@"ChargeAndBalanceCell" owner:nil options:nil];
            cell=array[1];
        }
    }
    
    if (indexPath.section==0)//我的余额
    {
        cell.cell1_headTitle.text=@"28元";
        cell.cell1_descTitle.text=@"消费记录";
        cell.cell1_descTitle.textColor=[UIColor lightGrayColor];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        
    }
    else if (indexPath.section==1)//选择充值金额
    {
        cell.cell1_headTitle.text=RechargeAmountArray[indexPath.row];
        cell.cell1_descTitle.text=GiveMoneyArray[indexPath.row];
        cell.cell1_descTitle.textColor=[UIColor colorWithRed:244/255.0 green:172/255.0 blue:90/255.0 alpha:1];
        if (indexPath.row==ChargeSelectCellIndex)
        {
            cell.accessoryType=UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType=UITableViewCellAccessoryNone;
        }
    }
    else if(indexPath.section==2)//选择支付方式
    {
        cell.selectionStyle=UITableViewCellSelectionStyleNone;//取消选中的颜色
        if (indexPath.row<3)
        {
            cell.cell1_headTitle.text=PayStyleArray[indexPath.row];
            cell.cell1_descTitle.text=PayDescArray[indexPath.row];
            cell.cell1_descTitle.textColor=[UIColor lightGrayColor];
            if (indexPath.row==2)
            {
                cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            }
            else if(indexPath.row==PaySelectCellIndex)
            {
                cell.accessoryType=UITableViewCellAccessoryCheckmark;
            }
            else
            {
                cell.accessoryType=UITableViewCellAccessoryNone;
            }
        }
        
    }
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:self.bounds];
    view.backgroundColor=[UIColor groupTableViewBackgroundColor];
    UILabel *sectionTitle=[[UILabel alloc] initWithFrame:CGRectMake(10, 2, 200, 26)];
    if (section==0)
    {
        sectionTitle.text=@"我的余额";
    }
    else if (section==1)
    {
        sectionTitle.text=@"选择充值金额";
    }
    else
    {
        sectionTitle.text=@"选择支付方式";
    }
    [view addSubview:sectionTitle];
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==1)
    {
        ChargeSelectCellIndex=indexPath.row;
    }
    else if (indexPath.section==2)
    {
        if (indexPath.row==3)
        {
            return;
        }
        PaySelectCellIndex=indexPath.row;
    }
    
        NSIndexSet *indexset=[[NSIndexSet alloc] initWithIndex:indexPath.section];
        [tableView reloadSections:indexset withRowAnimation:UITableViewRowAnimationNone];

}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
